package com.hs.dao.impl;

import com.hs.dao.DeptDao;
import com.hs.pojo.Dept;
import com.hs.pojo.Teacher;
import com.hs.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
    @Override
    public List<Dept> selectAll()  {
        String sql="select * from dept";
        Object obj[]={};
        ResultSet res = JDBCUtils.query(sql, obj);
        List<Dept> list = new ArrayList<>();
        while (true){
            try {
                if (!res.next())break;
                Dept dept=new Dept(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3)
                );
                list.add(dept);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();

        return list;
    }

    @Override
    public List<Dept> selectDept(int startIndex, int pageSize) {
        String sql="select * from dept limit ?,?";
        Object obj[]={startIndex,pageSize};
        ResultSet res = JDBCUtils.query(sql, obj);
        List<Dept> list=new ArrayList<>();
        while (true){
            try {
                if (!res.next())break;
                Dept dept=new Dept(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3)
                );
                list.add(dept);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();
        return list;
    }

    @Override
    public int selectCount() {
        String sql="select count(1) from dept";
        Object obj[]={};
        ResultSet res = JDBCUtils.query(sql, obj);
        int count=0;
        while (true){
            try {
                if (!res.next())break;
                count=res.getInt(1);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();
        return count;
    }
}
